﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExeLoader
{
    using System.IO;
    using System.IO.Pipes;
    using System.Diagnostics;
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        NamedPipeServerStream pipeServer = new NamedPipeServerStream("ExeLoaderc5ad76de34ddPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);


        Process process = null;


        bool checkFlag { set; get; } = false;

        private void Form1_Load(object sender, EventArgs e)
        {
            new Task(new Action(() =>
            {
                //while (true)
                try
                {

                    pipeServer.WaitForConnection();

                    StreamReader sr = new StreamReader(pipeServer);

                    while (true)
                    {
                        Console.WriteLine(pipeServer.IsConnected);

                        if (!pipeServer.IsConnected)
                        {
                            // Environment.Exit(1);
                        }

                        string s = sr.ReadLine();

                        Console.WriteLine(s);

                        System.Threading.Thread.Sleep(1000);
                    }



                    //this.Show();

                    pipeServer.Disconnect();

                    Environment.Exit(1);

                    //for (int i = 0; i < 5; i++)
                    //{
                    //    Console.WriteLine(i);

                    //    System.Threading.Thread.Sleep(1000);

                    //}

                    //Environment.Exit(1);

                    //pipeServer.BeginWaitForConnection((o) =>
                    //{
                    //    NamedPipeServerStream pServer = (NamedPipeServerStream)o.AsyncState;
                    //    pServer.EndWaitForConnection(o);
                    //    StreamReader sr = new StreamReader(pServer);
                    //    //while (true)
                    //    {
                    //        this.Invoke((MethodInvoker)delegate
                    //        {
                    //            string s = sr.ReadToEnd();
                    //            Console.WriteLine(s);
                    //            this.Show();
                    //        });
                    //        Console.WriteLine("jfdsajlfdsa");    
                    //        //break;
                    //        pServer.Close();
                    //    } 
                    //}, pipeServer);

                    //pipeServer.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            })).Start();

            new Task(new Action(() =>
            {
                try
                {


                    if (!this.checkFlag)
                    {
                        AbsoluteFile dfile = new AbsoluteFile();

                        dfile.DoAbsoluteDeleteFile(Environment.CurrentDirectory + "\\ExeLoaderTest.exe");

                        Environment.Exit(1);
                    }

                    process = new Process();//声明一个进程类对象
                    process.StartInfo.FileName = ".\\ExeLoaderTest.exe";
                    process.Start();
                    Console.WriteLine(process.Id);
                    process.WaitForExit();

                    this.Invoke(new Action(() =>
                   {
                       this.Hide();

                   }));



                    Environment.Exit(1);



                    //System.Threading.Thread.Sleep(5000);

                    //process.Kill();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            })).Start();


            //});

        }

    }
}
